package com.ctg.behavior.calc.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctg.behavior.calc.impala.calculator.CtPropertyCalculator;
import com.ctg.behavior.common.calc.CtCalcResult;
import com.ctg.behavior.common.properties.CtPropertyValuesCalcBody;
import com.ctg.behavior.common.properties.CtPropertyValuesCalcRequest;
import com.ctg.behavior.common.properties.CtPropertyValuesCalcResult;
import com.ctg.behavior.common.properties.CtUserPropertiesCalcBody;
import com.ctg.behavior.common.properties.CtUserPropertiesCalcRequest;
import com.ctg.behavior.common.properties.CtUserPropertiesCalcResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/property")
public class CtPropertyController {

	@Autowired
	private CtPropertyCalculator propertyCalculator;

	@PostMapping("/values")
	public CtPropertyValuesCalcResult values(@RequestBody CtPropertyValuesCalcRequest request) {
		CtPropertyValuesCalcBody body = request.getReq();
		return this.values(body);
	}

	public CtPropertyValuesCalcResult values(@RequestBody CtPropertyValuesCalcBody body) {

		CtPropertyValuesCalcResult result = new CtPropertyValuesCalcResult();

		// String event = CtCalcPropertyUtils.getProperty(body.getField());
		String appId = body.getAppId();
		String fuzzyStr = body.getFuzzyStr();
		Integer limit = body.getLimit();

		// 检查参数
		if (StringUtils.isEmpty(body.getField())) {
			log.error("field", body.getField());
			result.setSuccess(false);
			result.setErrorCode("400");
			result.setErrorMessage("");
			return result;
		}

		CtPropertyValuesCalcBody propertyCalcBody = new CtPropertyValuesCalcBody();
		propertyCalcBody.setField(body.getField());
		propertyCalcBody.setAppId(appId);
		if (!StringUtils.isEmpty(fuzzyStr)) {
			propertyCalcBody.setFuzzyStr(fuzzyStr);
		}
		if (limit == null || limit <= 0) {
			propertyCalcBody.setLimit(20);
		}
		propertyCalcBody.setLimit(limit);
		if (StringUtils.isEmpty(propertyCalcBody.getAppId())) {
			// TEMP
			propertyCalcBody.setAppId("154058");
		}
		CtCalcResult calcResult = propertyCalculator.doCalculator(propertyCalcBody);
		if (calcResult instanceof CtPropertyValuesCalcResult) {
			CtPropertyValuesCalcResult pvcResult = (CtPropertyValuesCalcResult) calcResult;
			return pvcResult;
		}

		return result;
	}

	@PostMapping("/user/values")
	public CtUserPropertiesCalcResult values(@RequestBody CtUserPropertiesCalcRequest request) {

		CtUserPropertiesCalcResult result = new CtUserPropertiesCalcResult();

		CtUserPropertiesCalcBody body = request.getReq();
		CtCalcResult calcResult = propertyCalculator.doCalculator(body);
		if (calcResult instanceof CtUserPropertiesCalcResult) {
			CtUserPropertiesCalcResult pvcResult = (CtUserPropertiesCalcResult) calcResult;
			return pvcResult;
		}

		return result;
	}

}
